package hr.fer.pus.dll_will.cr.actions;

import hr.fer.pus.dll_will.cr.IActionHandler;
import hr.fer.pus.dll_will.cr.data.FileDescriptor;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;

public class ListFiles implements IActionHandler {

	private List<FileDescriptor> files;
	
	public ListFiles(List<FileDescriptor> l){
		this.files = l;
	}
	
	@Override
	public boolean performAction(ServletOutputStream out,
			HttpServletRequest request) throws IOException {
		
		System.out.println("listing files...");
		if (files == null){
			// silent fail?
			return false;
		}
		
		StringBuilder sb = new StringBuilder();
		for(int i = 0; i < files.size(); ++i){
			sb.append(files.get(i).toString());
			System.out.println("file: " + files.get(i).toString());
			if((i+1) != files.size()) sb.append("#");
		}
		out.write(sb.toString().getBytes());
		return true;
	}

}
